package com.jsyh.maidu.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jsyh.maidu.R;
import com.jsyh.maidu.adapter.ArticleExpandableAdapter;
import com.jsyh.maidu.model.Article;
import com.jsyh.maidu.model.ArticleListModel;
import com.jsyh.maidu.model.ArticleModel;
import com.jsyh.maidu.presenter.ArticlePresenter;
import com.jsyh.maidu.views.ArticleView;

import java.util.List;

public class ArticleActivity extends BaseActivity implements ArticleView, View.OnClickListener{

    private ExpandableListView articleList;
    private ArticlePresenter presenter;
    List<Article> datas;
    ArticleExpandableAdapter adapter;
    TextView title;
    FrameLayout back;

    @Override
    public void initView() {
        super.initView();
        setContentView(R.layout.activity_article);
        articleList = (ExpandableListView) findViewById(R.id.article_list);
        articleList.setGroupIndicator(null);
        groupChildClick();
    }
    private void groupChildClick(){
        articleList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Intent intent = new Intent(ArticleActivity.this, ArticleListActivity.class);
                intent.putExtra("cat_id", datas.get(groupPosition).getChildren().get(childPosition).getCat_id());
                startActivity(intent);
                return true;
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new ArticlePresenter(this);
        presenter.getArticles(this);
    }

    @Override
    public void initTitle() {
        super.initTitle();
        title = (TextView) findViewById(R.id.title);
        back = (FrameLayout) findViewById(R.id.fl_Left);
        title.setText("文章分类");
        back.setOnClickListener(this);
    }

    @Override
    public void getArticles(ArticleModel model) {
        if (model.getCode().equals("1")){
            datas = model.getData();
            adapter = new ArticleExpandableAdapter(this, datas);
            articleList.setAdapter(adapter);
        }else {
            Toast.makeText(this, model.getMsg(),Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getArticleList(ArticleListModel model) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fl_Left:
                finish();
                break;
        }
    }
}
